package br.tce.ufs.appcoleta_mobile.database;

import java.sql.SQLException;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import br.tce.ufs.appcoleta_mobile.entities.FatorGerador;
import br.tce.ufs.appcoleta_mobile.entities.LocalidadeObra;
import br.tce.ufs.appcoleta_mobile.entities.ObraServico;
import br.tce.ufs.appcoleta_mobile.entities.TipoFatorGerador;
import br.tce.ufs.appcoleta_mobile.entities.TipoGeo;
import br.tce.ufs.appcoleta_mobile.entities.UnidadeGestora;
import br.tce.ufs.appcoleta_mobile.entities.Usuario;

public class DBHelper extends OrmLiteSqliteOpenHelper{
	
	private static String DATABASENAME = "coleta_mobile.db";
	private static int DATABASEVERSION = 3;

	public DBHelper(Context context) {
		super(context, DATABASENAME, null, DATABASEVERSION);
		// TODO Auto-generated constructor stub
	}
	
	public static ConnectionSource getConnectionSource(Context context) {
	    return new DBHelper(context).getConnectionSource();
	  }

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		// TODO Auto-generated method stub
		try{
			//TableUtils.createTable(connectionSource, Pessoa.class);
			TableUtils.createTable(connectionSource, UnidadeGestora.class);
			TableUtils.createTable(connectionSource, Usuario.class);
			TableUtils.createTable(connectionSource, TipoGeo.class);
			TableUtils.createTable(connectionSource, TipoFatorGerador.class);
			TableUtils.createTable(connectionSource, FatorGerador.class);
			TableUtils.createTable(connectionSource, ObraServico.class);
			TableUtils.createTable(connectionSource, LocalidadeObra.class);
			/*TableUtils.createTable(connectionSource, LocalidadeObra.class);
			TableUtils.createTable(connectionSource, GeoObra.class);
			TableUtils.createTable(connectionSource, FotoObra.class);*/
		}catch (SQLException e) {
			// TODO: handle exception
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2,
			int arg3) {
		try {
			//TableUtils.dropTable(connectionSource, Pessoa.class, true);
			TableUtils.dropTable(connectionSource, UnidadeGestora.class,true);
			TableUtils.dropTable(connectionSource, Usuario.class,true);
			TableUtils.dropTable(connectionSource, TipoGeo.class,true);
			TableUtils.dropTable(connectionSource, TipoFatorGerador.class,true);
			TableUtils.dropTable(connectionSource, FatorGerador.class,true);
			TableUtils.dropTable(connectionSource, ObraServico.class,true);
			/*TableUtils.dropTable(connectionSource, LocalidadeObra.class,true);
			TableUtils.dropTable(connectionSource, GeoObra.class,true);
			TableUtils.dropTable(connectionSource, FotoObra.class,true);*/
			onCreate(db,connectionSource);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	 public void close() {
	  super.close();
	 }

}
